import { APIDateBill, APIStatisticsData } from "@/api/interface"
import { reactive } from "vue"
import { city } from "@/utils/urbanDictionary"
import { orderFiltering } from "@/utils"

// 首页看板数据
export interface DashboardData {
  user_count: number //用户总数
  member_count: number //会员总数
  bill_count: number //订单数量
  bill_count_nopay: number //未支付订单
  bill_count_pay: number //已支付订单
  bill_sum_pay: number //支付总金额

  bill_time_count: number //以下是按时间统计的订单情况
  bill_time_count_nopay: number
  bill_time_count_pay: number
  bill_time_sum_pay: number
  member_city: [
    //城市分布情况
    {
      city?: string
      number_of_users?: number
      name?: string
      value?: number[]
    }
  ]
  member_male_count: number //男性会员
  member_female_count: number //女性会员

  member_today_count: number //今日新增会员数
  member_yesterday_count: number //昨日新增会员数

  bill_today_sum_pay: number //今日支付金额
  bill_yesterday_sum_pay: number //昨日支付金额
  bill_month_sum_pay: number //本月销售金额

  bill_today_count_pay: number //今日订单数量
  bill_yesterday_count_pay: number //昨日订单数量

  user_today_count: number //今日新增用户数
  user_yesterday_count: number //昨日新增用户数

  robotuser_month_count: number
  robotuser_today_count: number
  robotuser_yesterday_count: number

  robotuser_image_count: number
  robotuser_chat_count: number
  robotuser_tts_count: number
}

// interface DateBill {
//   [key: string]: {
//     bill_date_count_pay: number
//     bill_date_count_nopay: number
//     bill_date_count: number
//   }
// }
interface DateBill {
  x: string[]
  yNopayData: number[]
  yPayData: number[]
  yCount: number[]
}
interface State {
  dashboardData: DashboardData
  dateBill: DateBill
  loading: boolean
}

const state = reactive<State>({
  dashboardData: {} as DashboardData,
  dateBill: {} as DateBill,
  loading: false
})

export default function useDashboard() {
  //获取看板数据
  const getStatisticsData = async (timeData: any) => {
    state.loading = true
    let { data, code } = await APIStatisticsData(timeData)
    if (code === 200) {
      // 格式化城市数据
      data.member_city = data.member_city
        .filter((e: any) => city(e.city)?.name)
        .map((item: any) => {
          item = {
            name: city(item.city)?.name || "",
            value: city(item.city)?.value || "",
            number_of_users: item.number_of_users
          }
          return item
        })

      state.dashboardData = data
      state.loading = false
    }
  }
  //获取订s数据
  const getDateBill = async (timeData: any) => {
    state.loading = true
    let { data, code } = await APIDateBill(timeData)
    if (code === 200) {
      // console.log(data, 'data',orderFiltering(data));

      state.dateBill = orderFiltering(data)
    }
  }

  return { getDateBill, getStatisticsData, state }
}
